import { Viewer } from "cesium";
import * as Cesium from "cesium";
/**
 * @description wmts图层
 * @param {Viewer} viewer
 * @param {Cesium.WebMapTileServiceImageryProvider.ConstructorOptions} options
 * @param {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addWmtsLayer = (
  viewer: Viewer,
  options: Cesium.WebMapTileServiceImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.WebMapTileServiceImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};
/**
 * @description wms 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addWmsLayer = (
  viewer: Viewer,
  options: Cesium.WebMapServiceImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.WebMapServiceImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
}; /**
 * @description wm 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */

export const addWmLayer = (viewer: Viewer) => {
  console.log(viewer);
};
/**
 * @description Tms 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addTmsLayer = (viewer: Viewer) => {
  console.log(viewer);
  // new Cesium.TileMapServiceImageryProvider
};
/**
 * @description mvt 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addMvtLayer = (viewer: Viewer) => {
  console.log(viewer);
};
/**
 * @description DEM 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addDEMLayer = (viewer: Viewer) => {
  console.log(viewer);
};
/**
 * @description UrlTemplate 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addTemplateImagery = (
  viewer: Viewer,
  options: Cesium.UrlTemplateImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const imageryLayersMap = new Cesium.UrlTemplateImageryProvider(options);
    const layer = viewer.imageryLayers.addImageryProvider(
      imageryLayersMap,
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};
/**
 * @description 渲染 ArcGIS 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addArcGISLayer = (
  viewer: Viewer,
  options: Cesium.ArcGisMapServerImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.ArcGisMapServerImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};
/**
 * @description BingMap 图层
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addBingMapLayer = (
  viewer: Viewer,
  options: Cesium.BingMapsImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.BingMapsImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};
/**
 * @description MapBox
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addMapBoxLayer = (
  viewer: Viewer,
  options: Cesium.MapboxImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.MapboxImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};
/**
 * @description Single
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addSingleLayer = (
  viewer: Viewer,
  options: Cesium.SingleTileImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.SingleTileImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};

/**
 * @description S3M
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addS3MLayer = (viewer: Viewer) => {
  console.log(viewer);
};

/**
 * @description Google
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addGoogleLayer = (viewer: Viewer) => {
  console.log(viewer);
};

/**
 * @description mfs
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addmfsLayer = (viewer: Viewer) => {
  console.log(viewer);
};

/**
 * @description osm
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addOsmLayer = (
  viewer: Viewer,
  options: Cesium.OpenStreetMapImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.OpenStreetMapImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};

/**
 * @description ion
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addIonLayer = (
  viewer: Viewer,
  options: Cesium.IonImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.IonImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};
/**
 * @description TileMapService
 * @param {Viewer} viewer
 * @param  {} options
 * @param  {string} name
 * @param {number} [index]
 * @returns {Promise<Cesium.ImageryLayer>}
 */
export const addTileMapServiceLayer = (
  viewer: Viewer,
  options: Cesium.TileMapServiceImageryProvider.ConstructorOptions,
  name: string,
  index?: number
): Promise<Cesium.ImageryLayer> => {
  return new Promise<Cesium.ImageryLayer>((resolve) => {
    const layer = viewer.imageryLayers.addImageryProvider(
      new Cesium.TileMapServiceImageryProvider(options),
      index
    );
    Reflect.set(layer, "name", name);
    resolve(layer);
  });
};

/**
 * @description 删除影像图层
 * @param {Viewer} viewer
 * @param  {string} name 图层的名称
 */
export const removeImageryLayer = (viewer: Viewer, naem: string) => {
  // 获取所有图层
  const layerAll = (viewer.imageryLayers as any)._layers;

  // 通过图层名称过滤出目标图层
  const layer = layerAll.filter(
    (item: any) => Reflect.get(item, "name") == naem
  )[0];

  // 如果找到了目标图层
  if (layer) {
    viewer.imageryLayers.remove(layer);
  } else {
    console.error("没有找到图层");
  }
};
/**
 * @description 显示隐藏图层
 * @param {Viewer} viewer
 * @param  {string} name 图层的名称
 * @param  {boolean} isShow =true 是否显示
 */
export const displayHideLayer = (
  viewer: Viewer,
  naem: string,
  isShow: boolean = true
) => {
  let layerAll = (viewer.imageryLayers as any)._layers;
  let layer = layerAll.filter(
    (item: any) => Reflect.get(item, "name") == naem
  )[0];
  if (layer) {
    layer.show = isShow;
  } else {
    console.error("没有找到图层");
  }
};
